package leetcode_401_500;

import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class LeeCode_406 {
    public static void main(String[] args) {
        int[][] ints = reconstructQueue(new int[][]{{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}});
        System.out.println();
    }
    private static int[][] reconstructQueue(int[][] people) {
        Arrays.sort(people, (o1, o2) -> {
            if (o1[0] == o2[0]){
                return o1[1] - o2[1];
            }else {
                return o2[0] - o1[0];
            }
        });
        LinkedList<int[]> list = new LinkedList<>();
        for (int[] p : people) {
            if (p[1] >= list.size()){
                list.addLast(p);
            }else {
                list.add(p[1], p);
            }
        }
        int[][] ans = new int[people.length][2];
        int i = 0;
        for (int[] ints : list) {
            ans[i] = ints;
            i++;
        }
        return ans;
    }
}
